<template>
  <div class="app-container">
    <el-row justify="space-between" class="headline">
      <el-col :span="2">
        <span>数据列表</span>
      </el-col>
      <el-col :span="2">
        <el-button @click="staffAdd">添加</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="tableList">
      <el-table-column label="角色名称" align="center" prop="tableName" :show-overflow-tooltip="true" />
      <el-table-column label="角色描述" align="center" prop="tableComment" :show-overflow-tooltip="true" />
      <el-table-column label="添加时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" width="330" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-tooltip content="权限设置" placement="top">
            <el-button link type="primary" icon="setting" @click="handlePreview(scope.row)" v-hasPermi="['tool:gen:preview']"></el-button>
          </el-tooltip>
          <el-tooltip content="编辑" placement="top">
            <el-button link type="primary" icon="Edit" @click="handleEditTable(scope.row)" v-hasPermi="['tool:gen:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['tool:gen:remove']"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.page_count" v-model:limit="queryParams.page_size" @pagination="getList" />
    <!-- 预览界面 -->
    <el-dialog :title="preview.title" v-model="preview.open" width="35%" top="100vh" append-to-body class="scrollbar">
      <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="120px" class="demo-ruleForm" :size="formSize" status-icon>
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="ruleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="text">
          <el-input v-model="ruleForm.text" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item>
          <el-row :gutter="20">
            <el-button :span="6" :offset="6" @click="btnCancel"> 取消</el-button>
            <el-button :span="6" :offset="6" @click="btnConfirm"> 确定</el-button>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup name="Gen">
import { listTable, previewTable, delTable, genCode, synchDb } from "@/api/tool/gen";
import router from "@/router";

const route = useRoute();
const { proxy } = getCurrentInstance();
const formSize = ref("default");
const tableList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const tableNames = ref([]);
const dateRange = ref([]);
const uniqueId = ref("");

const data = reactive({
  queryParams: {
    page_count: 1,
    page_size: 10,
    tableName: undefined,
    tableComment: undefined,
  },
  preview: {
    open: false,
    title: "添加角色",
    data: {},
    activeName: "domain.java",
  },
});
const ruleForm = reactive({
  text: "", //描述
  name: "", //名称
});

const rules = reactive({
  text: [{ required: true, message: "请填写角色描述", trigger: "blur" }],
  name: [
    {
      required: true,
      message: "请填写角色名称",
      trigger: "blur",
    },
  ],
});

const { queryParams, preview } = toRefs(data);
// 添加
function staffAdd() {
  preview.value.open = true;
}
//弹窗取消
function btnCancel() {
  preview.value.open = false;
}
//弹窗确定
function btnConfirm() {
  preview.value.open = false;
  //done
}
onActivated(() => {
  const time = route.query.t;
  if (time != null && time != uniqueId.value) {
    uniqueId.value = time;
    queryParams.value.page_count = Number(route.query.page_count);
    dateRange.value = [];
    proxy.resetForm("queryForm");
    getList();
  }
});

/** 查询表集合 */
function getList() {
  loading.value = true;
  listTable(proxy.addDateRange(queryParams.value, dateRange.value)).then((response) => {
    tableList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.page_count = 1;
  getList();
}

/** 权限修改 */
function handlePreview(row) {
  // previewTable(row.tableId).then((response) => {
  // preview.value.data = response.data;
  // preview.value.open = true;
  // preview.value.activeName = "domain.java";
  // });
  router.push("setting");
}

/** 编辑按钮操作 */
function handleEditTable(row) {
  preview.value.open = true;
  // const tableId = row.tableId || ids.value[0];
}
/** 删除按钮操作 */
function handleDelete(row) {
  const tableIds = row.tableId || ids.value;
  proxy.$modal
    .confirm('是否确认删除表编号为"' + tableIds + '"的数据项？')
    .then(function () {
      return delTable(tableIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

getList();
</script>
<style lang="scss" scoped>
.headline {
  background-color: #f3f3f3;
  text-align: left;
  span {
    display: block;
    padding-top: 5px;
    margin-left: 5px;
  }
}
:deep(.el-overlay-dialog) {
  top: 500vh;
}
</style>
